package com.shyt.security;

import com.alibaba.fastjson.JSON;
import com.shyt.util.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/***
 * 匿名认证拒绝认证返回信息，返回匿名认证错误的信息
 */
@Component
@Slf4j
public class NMRZRejectUtil implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {
        log.info("进入匿名认证,匿名认证开始__________________________________");
        String result = JSON.toJSONString(
                ApiResponse.erro("410", "匿名认证失败", "匿名认证失败，请联系管理员"));

        ServletOutputStream outputStream = httpServletResponse.getOutputStream();

        outputStream.write(result.getBytes(StandardCharsets.UTF_8));


        //outputStream.write("匿名认证失败，请联系管理员".getBytes(StandardCharsets.UTF_8));

        outputStream.flush();
        outputStream.close();
        log.info("进入匿名认证,匿名认证结束__________________________________");
    }
}
